package com.kikt.fragmentdemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    protected FragmentManager fm;
    protected Fragment fragment1;
    @Bind(R.id.bt1)
    Button mBt1;
    @Bind(R.id.bt2)
    Button mBt2;
    @Bind(R.id.fl)
    FrameLayout mFl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        fm = getSupportFragmentManager();

        init();

        mBt1.setOnClickListener(this);
        mBt2.setOnClickListener(this);
    }


    private void init() {
        fragment1 = fm.findFragmentById(R.id.fragment);
    }

    @Override
    public void onClick(View v) {
        FragmentTransaction ft = fm.beginTransaction();
        switch (v.getId()) {
            case R.id.bt1:
                ft.replace(mFl.getId(), new Fragment1(), "f1");
                break;
            case R.id.bt2:
                ft.replace(mFl.getId(),new Fragment2(), "f2");
                break;
        }
        ft.commit();
    }
}
